jQuery(function($) {
	
	var doc  = document,
		doc_ = $(doc);
	
	function errorMessage(elm, msg) {
		elm.html(msg);
	}
	
	//弹出对话框
	function showElement(elm) {
		elm.addClass("show_");
		elm.removeClass("hide");
		
		$(".mirror").show();
	}
	//关闭所有图层
	function hideElement(elm) {
		elm.removeClass("show_");
		elm.addClass("hide_");
		
		$(".mirror").hide();
	}
	
	/*弹出约谈层*/
		/*start*/
		//ios兼容
	doc_.on("touchstart", ".head .y-btn", function() {
		event.preventDefault();
		showElement($(".talk"), $(".mirror"));
	});
	doc_.on("touchstart", ".close-btn", function() {
		event.preventDefault();
		var elm = $(".talk");
		
		elm.removeClass("show_");
		elm.addClass("hide_");
		
		$(".mirror").hide();
	});
	
	
	doc_.on("click", ".head .y-btn", function() {
		showElement($(".talk"));
	});
	
	doc_.on("click", ".close-btn", function() {
		var elm = $(".talk");
		
		elm.removeClass("show_");
		elm.addClass("hide_");
		
		$(".mirror").hide();
	});
	/*end*/
	
	/***弹出领投跟投层***/
	function clearMessage() {	//当点击取消和mirror层时清除领投跟投信息
		$(".test .error").html("");
		$(".test input").val("");
	}
		//兼容ios
	doc_.on("touchstart", ".data .cv-0", function() {
		event.preventDefault();
		showElement($(".test"));
		$(".t-1").show(); //显示领投层
		$(".t-0").hide(); //关闭跟投层
	});
	doc_.on("touchstart", ".test .t-1 .close-btn", function() {
		hideElement($(".test"));
		clearMessage();
	});
	
	
	doc_.on("click", ".data .cv-0", function() {
		showElement($(".test"));
		
		$(".t-1").show(); //显示领投层
		$(".t-0").hide(); //关闭跟投层
	});
	
	doc_.on("click", ".test .t-1 .close-btn", function() {
		hideElement($(".test"));
		clearMessage();
	});
	
		//弹出跟投层
		//start
		//兼容ios
	doc_.on("touchstart", ".data .cv1", function(event) {
		event.preventDefault();
		showElement($(".test"));
		$(".t-0").show(); //显示领投层
		$(".t-1").hide(); //关闭跟投层
	});
	
	doc_.on("touchstart", ".t-0 .close-btn", function() {
		hideElement($(".test"));
		clearMessage();
	});
	
	doc_.on("click", ".data .cv1", function() {
		event.preventDefault();
		showElement($(".test"));
		
		$(".t-0").show(); //显示领投层
		$(".t-1").hide(); //关闭跟投层
	});
	
	doc_.on("click", ".t-0 .close-btn", function() {
		hideElement($(".test"));
		clearMessage();
	});
	
		//验证领投跟层
	/*var done = {
		num: false
	};*/
	
	var time = {
		timer: 0
	};
	
	function hideBox(err, msg) {
		clearInterval();
		time.timer = setTimeout(function() {
			hideElement($(".test"));
			errorMessage(err, msg);
		}, 1000);
	}
	
	function checkIsNumber(elm, err) {
		
		var re = /^([1-9][1-9]*)$/g,	//用于判断是否为数值
			re2 = /^([0])/g,
			val = elm.val();
		
		if (re2.test(val)) {
			errorMessage(err, "投资金额必须大于0且不能为负！");
			return;
		}
		
		if (val === "") {
			//done.num = false;
			errorMessage(err, "投资金额不能为空！");
			return;
		}
		
		if (re.test(val)) {
			errorMessage(err, "投资成功！");
			elm.val("");

			//0.5秒后关闭对话框
			hideBox(err, "");
			//done.num = true;
		} else {
			errorMessage(err, "投资金额必须为数字！");
		}
	}
	
	doc_.on("click", ".t-1 .sure-btn", function() {
		checkIsNumber($(".t-1 input"), $(".test .t-1 .error"));
	});
	
	doc_.on("click", ".t-0 .sure-btn", function() {
		checkIsNumber($(".t-0 input"), $(".test .t-0 .error"));
	});

	/****end****/
	
	/*领投跟投 cv-0领投 cv1跟投 cv2商业计划书*/
	//弹出领投层
	doc_.on("click", ".cv-0", function() {
		//console.log(1);
	});
	
	//点击背景关闭所有图层
	doc_.on("click", ".mirror", function() {
		hideElement($(".talk"));
		hideElement($(".test"));
	});
	

	/*交互动画*/
	var M = {};
	
	/*图片轮播*/
	var M = {},
		doc = document;

	M.create = function(elm) {
		return doc.createElement(elm);
	};

	M.Slide = function() {
		var that = this;
		that.frame = doc.querySelector(".slide");
		that.list = doc.querySelectorAll(".slide .nav li");
		that.images = doc.querySelectorAll(".gallery li");
		that.slideBox = doc.querySelector(".slide");
		that.navBox = doc.querySelector(".slide .nav");
		that.len = that.navBox.querySelectorAll("li").length;
		that.pre = doc.querySelector(".pre");
		that.next = doc.querySelector(".next");
		that.timer = 0;
		that.index = 0;
		
	};
	
	slide = M.slide = new M.Slide();
	
	slide.setIndex = function(list) {
		var that = this,
			len = list.length,
			i = 0;
			
		for (i; i < len; i++) {
			list[i].index = i;
			
		}
		list[0].className = "active";
		that.len = len;
	};
	

	slide.appendElm = function() {
		var that = this,
			navBox = that.navBox,
			images = that.images,
			len = that.images.length,
			i = 0,
			fragment = doc.createDocumentFragment();
			
		for (i; i < len; i++) {
			fragment.appendChild(M.create("li"));
		}
		navBox.appendChild(fragment);
		slide.setIndex(doc.querySelectorAll(".slide .nav li"));
	};
	slide.appendElm();
	
	slide.reElement = function(index) {
		var that = this,
			images = that.images,
			len = images.length,
			list = doc.querySelectorAll(".slide .nav li"),
			i = 0;
			
		for (i; i < len; i++) {
			images[i].className = "fadeout";
			list[i].className = "";
		}

		images[index].className = "fadein";
		list[index].className = "active";

	};
	
	slide.eventHandler = function(event) {
		var target = event.target;
		
		if (target.tagName === "LI") {
			slide.index = target.index;
			slide.reElement(target.index);
		}
	};
	
	slide.autoPlay = function() {
		clearInterval(slide.timer);
		slide.timer = setInterval(function() {
			slide.index++;
			if (slide.index >= slide.len) {
				slide.index = 0;
			}
			slide.reElement(slide.index);
		}, 3000);
	};
	slide.autoPlay();
	
	slide.navBox.addEventListener("mouseover", function(event) {
		slide.eventHandler(event);
	}, false);
	
	slide.frame.addEventListener("mouseover", function() {
		clearInterval(slide.timer);
	}, false);
	
	slide.frame.addEventListener("mouseout", function() {
		slide.autoPlay();
	}, false);
	
	slide.pre.addEventListener("click", function() {
		slide.index--;
		if (slide.index <= 0) {
			slide.index = slide.len - 1;
		}
		slide.reElement(slide.index);
	}, false);
	
	slide.next.addEventListener("click", function() {
		slide.index++;
		if (slide.index >= slide.len) {
			slide.index = 0;
		}
		slide.reElement(slide.index);
	}, false);
	
	
	
	//浮动右侧边栏
	M.getScrollTop = function() {
		return doc.body.scrollTop || doc.documentElement.scrollTop;
	};
	
	M.FixedElm = function() {
		var that = this;
		that.col2 = doc.querySelector(".col-2");
		that.lis  = doc.querySelectorAll(".col-2 li");
		that.offsetTop = that.col2.offsetTop;
	};
	
	//实列
	fe = M.fixedElm = new M.FixedElm();
	
	//计算元的offsettop并浮动元素
	fe.posElement = function() {
		var that = this,
			offsetTop = that.offsetTop;
			
		if ( (M.getScrollTop() - offsetTop) >= 0) {
			that.col2.style.cssText = "position: fixed; top: 0;";
		} else {
			that.col2.style.cssText = "position: static;";
		}
	};
	
	window.addEventListener("scroll", function(event) {
		fe.posElement();
	}, false);
	
	//切换列表
	M.timer = 0;
	
	M.scrollVal = {
		val: 0,
		valb: 0
	};
	
	M.scrollBack = function(sectionTop, scrolltop) {
		
		M.scrollVal.sectionTop = sectionTop;
		clearInterval(M.timer);
		
		M.timer = setInterval(function() {
			M.scrollVal.valb = M.getScrollTop() - 100;
			if (M.scrollVal.valb !== sectionTop) {
				window.scrollTo(0, M.scrollVal.valb);
			}
			
			if ( M.scrollVal.valb <= sectionTop ) {
				window.scrollTo(0, sectionTop);
				clearInterval(M.timer);
			}
		}, 15);
	};
	
	M.scrollElement = function(sectionTop, scrolltop) {
		M.scrollVal.sectionTop = sectionTop;
		clearInterval(M.timer);
		M.timer = setInterval(function() {
			
			M.scrollVal.val = M.getScrollTop() + 100;
			
			if (M.scrollVal.val !== sectionTop) {
				window.scrollTo(0, M.scrollVal.val);
			}
			
			if (M.scrollVal.val >= sectionTop) {
				window.scrollTo(0, sectionTop);
				clearInterval(M.timer);
			}
		}, 15);
	};
	
	M.tab = function(event) {
		var target = event.target,
			lis    = Array.prototype.slice.call(fe.lis),
			classname = target.className,
			tagName = target.tagName;
		
		if (classname.indexOf("li-") > -1) {
			lis.forEach(function(item, i) {
				item.className = "li-"+(i+1);
				item.index = i;
			});
			
			target.className += " selected";
			
			var sectionTop = doc.querySelectorAll(".contain_ section")[target.index].offsetTop;
			
			//滚动到列表对应的位置
			if (sectionTop - M.getScrollTop() > 0) {
				M.clearTimer();
				M.scrollElement(sectionTop, M.getScrollTop());
			} else {
				M.clearTimer();
				M.scrollBack(sectionTop, M.getScrollTop());
			}
		}
	};
	
	M.clearTimer = function() {
		setTimeout(function() {
			clearInterval(M.timer);
		}, 500);
	};
	
	fe.col2.addEventListener("click", function(event) {
		M.tab(event);
	}, false);
	
	//弹出分享框
	doc_.on("click", ".share a", function() {
		var elm = $(".jiathis_style_32x32");
		
		elm.addClass("rising");
		$(".mirror").show();
		
		setTimeout(function() {
			elm.removeClass("down");
		}, 300);
	});
	
	doc_.on("click", ".mirror", function() {
		var elm = $(".jiathis_style_32x32");
		
		elm.addClass("down");
		$(".mirror").hide();
		
		setTimeout(function() {
			elm.removeClass("rising");
		}, 300);
	});
	
});